package com.zondy.mapgis.psmap.manager.controller;

import cn.hutool.core.io.IoUtil;
import com.zondy.mapgis.common.controllerprefix.annotation.ManagerRestController;
import com.zondy.mapgis.common.core.web.controller.BaseController;
import com.zondy.mapgis.common.core.web.domain.AjaxResult;
import com.zondy.mapgis.common.core.web.page.TableDataInfo;
import com.zondy.mapgis.common.log.annotation.Log;
import com.zondy.mapgis.common.log.enums.BusinessType;
import com.zondy.mapgis.common.security.annotation.RequiresPermissions;
import com.zondy.mapgis.common.security.utils.SecurityUtils;
import com.zondy.mapgis.domain.psmap.AppTheme;
import com.zondy.mapgis.psmap.manager.service.IAppThemeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Tag(
   name = "主题管理",
   description = "主题控制器"
)
@ManagerRestController({"/app/theme"})
public class AppThemeController extends BaseController {
   @Autowired
   private IAppThemeService appThemeService;

   @Operation(
      summary = "查询主题列表"
   )
   @PreAuthorize("@ss.hasPermi('app:theme:list')")
   @RequiresPermissions({"app:theme:list"})
   @GetMapping({"/list"})
   public TableDataInfo list(AppTheme appTheme) {
      this.startPage();
      List<AppTheme> list = this.appThemeService.selectAppThemeList(appTheme);
      return this.getDataTable(list);
   }

   @Operation(
      summary = "导入主题列表"
   )
   @PreAuthorize("@ss.hasPermi('app:theme:import')")
   @RequiresPermissions({"app:theme:import"})
   @Log(
      title = "主题管理",
      businessType = BusinessType.IMPORT
   )
   @PostMapping({"/importData"})
   public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
      List<AppTheme> appThemeList = this.appThemeService.parseThemesFromFile(file, updateSupport);
      String operName = SecurityUtils.getUsername();
      String message = this.appThemeService.importThemes(appThemeList, updateSupport, operName);
      return this.success(message);
   }

   @Operation(
      summary = "下载导入模板"
   )
   @PostMapping({"/importTemplate"})
   public void importTemplate(HttpServletResponse response) {
   }

   @Operation(
      summary = "导出主题列表"
   )
   @PreAuthorize("@ss.hasPermi('app:theme:export')")
   @RequiresPermissions({"app:theme:export"})
   @Log(
      title = "主题管理",
      businessType = BusinessType.EXPORT
   )
   @PostMapping({"/export"})
   public void export(HttpServletResponse response, AppTheme appTheme) throws IOException {
      List<AppTheme> list = this.appThemeService.selectAppThemeList(appTheme);
      byte[] data = this.appThemeService.exportThemes(list);
      this.genCode(response, data);
   }

   @Operation(
      summary = "获取主题详细信息"
   )
   @PreAuthorize("@ss.hasPermi('app:theme:query')")
   @RequiresPermissions({"app:theme:query"})
   @GetMapping({"/{themeId}"})
   public AjaxResult getInfo(@PathVariable("themeId") Long themeId) {
      return this.success(this.appThemeService.selectAppThemeByThemeId(themeId));
   }

   @Operation(
      summary = "新增主题"
   )
   @PreAuthorize("@ss.hasPermi('app:theme:add')")
   @RequiresPermissions({"app:theme:add"})
   @Log(
      title = "主题管理",
      businessType = BusinessType.INSERT
   )
   @PostMapping
   public AjaxResult add(@RequestBody AppTheme appTheme) {
      if (!this.appThemeService.checkThemeNameUnique(appTheme.getThemeName())) {
         return this.error("新增主题'" + appTheme.getThemeName() + "'失败，主题名称已存在");
      } else {
         appTheme.setCreateBy(SecurityUtils.getUsername());
         return this.toAjax(this.appThemeService.insertAppTheme(appTheme));
      }
   }

   @Operation(
      summary = "修改主题"
   )
   @PreAuthorize("@ss.hasPermi('app:theme:edit')")
   @RequiresPermissions({"app:theme:edit"})
   @Log(
      title = "主题管理",
      businessType = BusinessType.UPDATE
   )
   @PutMapping
   public AjaxResult edit(@RequestBody AppTheme appTheme) {
      appTheme.setUpdateBy(SecurityUtils.getUsername());
      return this.toAjax(this.appThemeService.updateAppTheme(appTheme));
   }

   @Operation(
      summary = "删除主题"
   )
   @PreAuthorize("@ss.hasPermi('app:theme:remove')")
   @RequiresPermissions({"app:theme:remove"})
   @Log(
      title = "主题管理",
      businessType = BusinessType.DELETE
   )
   @DeleteMapping({"/{themeIds}"})
   public AjaxResult remove(@PathVariable Long[] themeIds) {
      return this.toAjax(this.appThemeService.deleteAppThemeByThemeIds(themeIds));
   }

   private void genCode(HttpServletResponse response, byte[] data) throws IOException {
      response.reset();
      response.addHeader("Access-Control-Allow-Origin", "*");
      response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
      response.setHeader("Content-Disposition", "attachment; filename=\"mapgis.zip\"");
      response.addHeader("Content-Length", "" + data.length);
      response.setContentType("application/octet-stream; charset=UTF-8");
      IoUtil.write(response.getOutputStream(), false, data);
   }
}
